from PyQt5.QtWidgets import QApplication,QWidget, QLabel, QVBoxLayout,QDesktopWidget
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import QTimer
from util.config import Config
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt

class WelcomeScreen(QWidget):
    def __init__(self, switch_to_login):
        super().__init__()
        self.switch_to_login = switch_to_login
        
        self.init_ui()
        
    
    def init_ui(self):
        
        self.setWindowTitle("欢迎界面")
        self.setWindowFlag(Qt.FramelessWindowHint)  # 无边框
        #self.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint)
        #self.setGeometry(0, 0, 800, 600)
        self.setFixedSize(600, 400)
        
        self.config = Config()
        self.setWindowIcon(QIcon(self.config.ICON_PATH))
        
        #self.setFixedSize(1000, 600)  # 设置窗口大小为400x300
         # 初始化配置
        
        # 设置布局
        self.layout = QVBoxLayout(self)

        # 设置 GIF 动画
        self.gif_label = QLabel(self)
        self.movie = QMovie(self.config.WELCOME_BG)  # 确保路径正确
        self.gif_label.setGeometry(0, 0, self.width(), self.height())
        self.gif_label.setScaledContents(True)#自适应内容
        self.gif_label.setMovie(self.movie)
        self.layout.addWidget(self.gif_label)

        # 播放动画
        self.movie.start()

        # 定时器，5 秒后切换到登录界面
        QTimer.singleShot(3000, self.switch_to_login)
        

        self.setLayout(self.layout)

    #解决设置无边框不能拖动
    def mousePressEvent(self, event):
        # 记录鼠标按下时的位置
        self.offset = event.pos()
 
    def mouseMoveEvent(self, event):
        # 移动窗口位置
        x = event.globalX()
        y = event.globalY()
        x_w = self.offset.x()
        y_w = self.offset.y()
        self.move(x - x_w, y - y_w)

